/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Effects
{
	import Util.Utils;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	public class Effect
	{
		public function Effect()
		{
		}
		
		public function get dt():Number { return 1 / stage.frameRate; }
		
		private var stage:Stage;
		
		private function Init(d:DisplayObject):void
		{
			if( d.stage != null ) stage = d.stage;
		}
		
		protected var contOn:Boolean =false;
		public function set ContOn(value:Boolean):void { contOn = value; }
		
		
		private var speedForwards:Number  = 1;
		private var speedBackwards:Number = 1;
		public function set SpeedForwards(value:Number):void { speedForwards = value; }
		public function set SpeedBackwards(value:Number):void { speedBackwards = value; }
		
		private function OnForwards(e:Event):void
		{
		  	Init(e.currentTarget as DisplayObject);
		  	RequireUpdates(e.currentTarget as DisplayObject, true);
		}
		
		private function OnBackwards(e:Event):void
		{
			RequireUpdates(e.currentTarget as DisplayObject, false);
		}
		
		public function RegisterEvents(d:DisplayObject, typeForwards:String, typeBackwards:String):void
		{
			d.addEventListener(typeForwards, OnForwards, false, 0, true);
			d.addEventListener(typeBackwards, OnBackwards, false, 0, true);
		}
		
		public function RegisterMouseOver(d:DisplayObject):void
		{
			RegisterEvents(d, MouseEvent.MOUSE_OVER, MouseEvent.MOUSE_OUT);
		} 
		
		public function RegisterFocus(d:DisplayObject):void
		{
			RegisterEvents(d, FocusEvent.FOCUS_IN, FocusEvent.FOCUS_OUT); 
		}
		
		private var ignoreNext:Boolean = false;
		public function RegisterVisible(d:DisplayObject):void
		{
			d.addEventListener("visible", function(e:Event):void
										  {
											if( ignoreNext == true ) ignoreNext = false;
											
											else
											{
												var c:DisplayObject = e.currentTarget as DisplayObject;												
											 	Init(c);
												RequireUpdates(d, c.visible, true);
												
												if( c.visible == false )
												{
													ignoreNext = true;
													 c.visible = true;
												}			
											}
										  }
										  , false, 0, true);
		}
		
		//generic event function
		public function RegisterEnabled(m:MovieClip):void
		{
			m.addEventListener("enabled", function(e:Event):void
										  {
										  	var m:MovieClip = e.currentTarget as MovieClip;	
										    Init(m);
											RequireUpdates(m, m.enabled);
										  }
										  , false, 0, true);
		}
		
		public function Reset(d:DisplayObject, forwards:Boolean):void
		{
			if( d == null ) return;
			Init(d);
			Update(d, forwards? 1 : 0, forwards, GetUserData(d, forwards));
		}
		
		public function Play(d:DisplayObject, forwards:Boolean):void
		{
			if( d == null ) return;
			Init(d);
			RequireUpdates(d, forwards);//very similar to below, can vis be moved?
		}
		
		
		
		private var arrUpdate:Array = new Array();
		
		protected function RequireUpdates(d:DisplayObject, forwards:Boolean, isVisibilityEffect:Boolean = false):void
		{
			var data:Data;
			var len:int = arrUpdate.length;
			for(var i:int = 0 ; i < len ; i++)
				if( Data(arrUpdate[i]).d == d )
				{
					data = arrUpdate[i];
					break;
				}
			
			if( data == null )
			{
				data = new Data(d, forwards, forwards? 0 : 1, GetUserData(d, forwards), isVisibilityEffect, contOn);
				arrUpdate.push(data);
				
				stage.addEventListener(Event.ENTER_FRAME, OnEnterFrame);
			}
			
			else
			{
				data.forwards = forwards;
			}
		}
		
		private function OnEnterFrame(e:Event):void
		{		
			for(var i:int = 0 ; i < arrUpdate.length ; i++)
			{
				var data:Data = Data(arrUpdate[i]);	
		
				var remove:Boolean = data.Step(dt, data.forwards? speedForwards : -speedBackwards); 
				
				Update(data.d, data.pos, data.forwards, data.user);
			
				if( remove )
				{
					if( data.pos == 0 && data.isVisibilityEffect )
					{
						ignoreNext = true;
						data.d.visible = false;
					}
					
					Utils.ArrayDeleteIndex(arrUpdate, i);
					i--;
				}
			}
			
			if( arrUpdate.length == 0 )
				stage.removeEventListener(Event.ENTER_FRAME, OnEnterFrame);
		}
		
		protected function GetUserData(d:DisplayObject, forwards:Boolean):Object
		{ 
			return null;
		}
		
		protected function Update(d:DisplayObject, pos:Number, forwards:Boolean, user:Object):void
		{
			
		}
		
	}
}

import flash.display.DisplayObject;
	
class Data
{
	public function Data(d:DisplayObject, forwards:Boolean, pos:Number, user:Object, isVisibilityEffect:Boolean, contOn:Boolean)
	{
		this.d = d;
		this.forwards = forwards;
		this.user = user;
		this.pos = pos;
		this.isVisibilityEffect = isVisibilityEffect;
		this.contOn = contOn;
	}
	
	public var isVisibilityEffect:Boolean;
	public var d:DisplayObject;
	public var forwards:Boolean;
	public var pos:Number;
	public var user:Object;
	public var contOn:Boolean;

	
	public function Step(dt:Number, speed:Number):Boolean //true if complete
	{
		pos += (speed * dt);
		
		if( pos <= 0 ) { pos = 0; return true; }
		if( pos >= 1 )
		{
			pos = 1;
			if( contOn ) return false;
			return true;
		}
		
		return false;
	}
}








